package com.qy127.controller;


import com.qy127.dao.BookDao;
import com.qy127.entity.Book;
import com.qy127.util.ResultEntity;
import com.qy127.util.OSSUploadUtil;
import com.qy127.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/book")
public class BookController {

    /**
     * 注入BookDao 接口
     */
    @Resource(name ="bookDaoImpl")
    private BookDao bookDao;

    /**
     * 注入Result类
     */
/*    @Resource(name = "result")
    private Result result;*/



    /**
     * 跳转页面
     * @return
     */
    @RequestMapping("/forwardBook")
    public String forwardBook(){
        return "forward:/WEB-INF/pages/showbook.jsp";
    }

    /**
     * 查询全部数据 和 查询部分数据的 转发
     * @param book
     * @return
     */
    @RequestMapping("/selectBook")
    public @ResponseBody Result selectBook(Book book){
        List<Map<String, Object>> allBook = bookDao.getAllBook(book);
        int getcount = bookDao.getcount(book);
        Result result = new Result(getcount, allBook);
//        result.setData(allBook);
        return result;
    }

    /**
     * 修改上下架状态
     * @param bookid
     * @param status
     * @return
     */
    @RequestMapping("/updateStat")
    public @ResponseBody String updateStatus(Integer bookid,Integer status){
        int i = bookDao.updataStatus(bookid, status);
        if (i>0){
            return "1001";
        }else {
            return "1002";
        }
    }

    /**
     * 新增或修改
     * @param book
     * @return
     */

    @RequestMapping("/addBook")
   /* public @ResponseBody String updateAndInsertBook(Book book){
        Integer bookid = book.getBookid();
        int i = 0;
        if (bookid == null){
            i = bookDao.insertOneBook(book);
        }else{
            i = bookDao.updateBook(book);
        }
        //System.out.println(i);
        if (i>0){
            return "1001";
        }else {
            return "1002";
        }
    }*/


    public  @ResponseBody Map<Object, Object> addBook(Book book) {
        HashMap<Object, Object> map = new HashMap<>();
        int i = 0;
        if (book.getBookid() == null || "".equals(book.getBookid())) {
            List<Map<String, Object>> maps = bookDao.queryOneBookByBooknamePublisher(book.getBookname(), book.getPublisher());

            //System.out.println("1---------"+maps);
            //System.out.println(maps.size());
            if (maps.size() > 0) {
                //System.out.println(1111);
                map.put("code",1003);
                map.put("msg","不能插入相同书名的相同出版社的书");
            } else {
                i = bookDao.insertOneBook(book);
                if (i > 0) {
                    map.put("code",1001);
                    map.put("msg","操作成功");
                }
            }
        } else {
            i = bookDao.updateBook(book);
            if (i > 0) {
                map.put("code",1001);
                map.put("msg","操作成功");
            }
        }
          if (i<0){
                map.put("code",1002);
                map.put("msg","操作失败");
            }
        return map;
    }

    /**
     * 上传文件 书籍图片
     * @param file
     * @param book
     * @return
     */
    @RequestMapping("/upload")
    public @ResponseBody Map upload(MultipartFile file,Book book){
        String s = OSSUploadUtil.upload2(file);
        //System.out.println(s);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("url",s);
        return map;

    }


}
